package zhtt.controller;

import com.gitee.zhtt.base.DateTimeUtils;
import com.gitee.zhtt.entity.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhtt.entity.Article;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @ClassName : LoginUserControlller  //类名
 * @Description :   //描述
 * @Author : zhtt  //作者
 * @Date: 2022-04-16 19:02  //时间
 */
@RestController
public class LoginUserController {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @PostMapping(value = "/login")
    public JsonResponse login(@RequestBody Article article) {
        httpServletRequest.getSession().setAttribute("loginUser", article);
        return JsonResponse.success("登录成功！");
    }

    @GetMapping(value = "/login")
    public JsonResponse getlogin(@RequestParam String name, @RequestParam int age) {
        System.out.println("login: "+DateTimeUtils.format(new Date()));
        Article article = new Article();
        article.setName(name);
        article.setAge(age);
        httpServletRequest.getSession().setAttribute("loginUser", article);
        return JsonResponse.success("登录成功！");
    }

    @GetMapping(value = "/getLoginUser")
    public JsonResponse getLoginUser() {
        System.out.println("getLoginUser: "+DateTimeUtils.format(new Date()));
        Article article = (Article) httpServletRequest.getSession().getAttribute("loginUser");
        /*if (article == null) {
            article = new Article();
        }*/
        return JsonResponse.success(article);
    }
}
